<template>
  <div class="customerManage">
    <view-header :title="'公告通知'"></view-header>
    <div class="search_area">
      <el-row>
        <el-form :form="queryForm" label-width="1.2rem">
          <el-col :span="6">
            <el-form-item label="创建时间：">
              <el-date-picker
                style="width: 100%"
                v-model="queryForm.createTime" clearable
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>

        </el-form>
        <el-col :span="6">
          <div class="btn_area">
            <el-button class="confirm" @click="btnClick">查询</el-button>
            <el-button class="cancle" @click="reset">重置</el-button>
            <el-button class="confirm" @click="add">新增</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <table-list :fields="fields" :list="list" :pagination="pagination" :total="total" @sizeChange="handleSizeChange" @currentChange="handleCurrentChange" @handle="onHandle" :panduan="true"></table-list>
    <el-dialog :title="code == '1' ? '新增公告' : '编辑公告'" :visible.sync="visible" width="60%" @close="close">
      <el-row>
        <el-form :form="noticeForm" label-width="1rem">
          <el-col :span="11">
            <el-form-item label="通知标题：">
              <el-input type="text" v-model="noticeForm.noticeTitle"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="通知内容：">
              <el-input type="textarea" v-model="noticeForm.noticeContent"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="附件上传：">
              <!-- <el-input type="textarea" v-model="noticeForm.noticeContent"></el-input> -->
              <el-button @click="uploadBtnClick" type="primary" style="margin:0;height: 0.32rem;line-height: 0;">上 传 附 件</el-button>
              <span style="margin-left: 0.1rem;cursor: pointer;" v-if="fileNames.length == 1">{{ fileNames[0]}}
              </span>
              <input style="position: absolute;left: -999px;" type="file" accept="doc/docx" multiple id="uploadImg" @change="uploadImgChange($event)"/>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="接收宿舍楼：">
              <el-select style="width: 100%;" v-model="noticeForm.buildingIdList" multiple collapse-tags clearable placeholder="请选择接收公告的宿舍楼">
                <el-option v-for="item in buildingList" :key="item.uuid" :value="item.uuid" :label="item.buildingName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible = false">取 消</el-button>
        <el-button class="confirm" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="公告详情" :visible.sync="visible2" width="30%" @close="close">
      <el-descriptions direction="vertical" :column="2" border>
        <el-descriptions-item label="公告标题" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ noticeForm.noticeTitle }}</el-descriptions-item>
        <el-descriptions-item label="公告内容" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ noticeForm.noticeContent }}</el-descriptions-item>
        <el-descriptions-item label="创建人" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ noticeForm.createByName }}</el-descriptions-item>
        <el-descriptions-item label="创建时间" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ noticeForm.createTime }}</el-descriptions-item>
        <el-descriptions-item label="公告附件" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">
          <el-tooltip content="点击下载" placement="top" effect="light">
            <span @click="downLoad" style="cursor: pointer;">
              {{ noticeForm.noticeFile }}
              <i class="el-icon-download"></i>
            </span>
          </el-tooltip>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>
<script>
import tableList from '@/components/table.vue'
import viewHeader from '@/components/viewHeader.vue'
export default {
  name: 'customerManagement',
  components: {
    tableList,
    viewHeader
  },
  data() {
    return {
      queryForm: {
		    "createTime": "",
      },
      list: [],
      fields: [
        {
          prop: 'rowNum',
          label: '序号',
          align: 'center'
        },
        {
          prop: 'noticeTitle',
          label: '公告标题',
          align: 'center'
        },
        {
          prop: 'publishFlagName',
          label: '发布状态',
          align: 'center'
        },
        {
          prop: 'createTime',
          label: '创建时间',
          align: 'center'
        },
        {
          prop: 'createByName',
          label: '创建人',
          align: 'center'
        },
        {
          prop: 'oper',
          label: '操作',
          align: 'center',
          btns: []
        }
      ],
      pagination: {
        currentPage: 1,
        pageSize: 10
      },
      total: 0,
      user: {},
      noticeForm: {
      },
      visible: false,
      visible2: false,
      code: '',
      fileNames: [],
      buildingList: []
    }
  },
  methods: {
    downLoad() {
      let url = this.$api.common.downLoad
      let formData = new FormData();
      formData.append('fileName',this.noticeForm.noticeFile)
      this.$http(url,formData,true,true).then(res => {
        let blob = new Blob([res.data]);
        let downloadElement = document.createElement("a");
        document.body.appendChild(downloadElement);
        let href = window.URL.createObjectURL(blob);
        downloadElement.href = href;
        downloadElement.download = this.noticeForm.noticeFile;
        downloadElement.click();
        document.body.removeChild(downloadElement);
        window.URL.revokeObjectURL(href);        
      })
    },
    uploadBtnClick() {
      document.getElementById('uploadImg').click();
    },
    uploadImgChange() {
      let files_obj = document.getElementById('uploadImg')
      let all_files_objs = [...files_obj.files];
      this.fileNames = all_files_objs.map(item => item.name)
      let formData = new FormData();
      for (let k = 0; k < all_files_objs.length; k++) {
        console.log(all_files_objs[k]);
        formData.append('file', all_files_objs[k]);
      }
      let url = this.$api.common.upload;
      this.$http(url, formData,false,true).then(res => {
        if(res.data.code == 200) {
          console.log(res.data);
          this.noticeForm.noticeFile = res.data.data
        }
      })
      console.log(all_files_objs)
      console.log(this.fileNames);
    },
    confirm() {
      let url = this.code == '1' ? this.$api.notice.create : this.$api.notice.update;
      if (this.code == '1') {
        this.noticeForm.createBy = this.user.uuid;
      }
      this.$http(url,this.reqData(this.noticeForm)).then(res => {
        if (res.data.code == 200) {
          this.$message.success(res.data.message);
          this.visible = false;
          this.queryNoticeList();
        } else {
          this.$message.error(res.data.message)
        }
      })
      console.log(this.noticeForm);
    },
    add() {
      this.visible = true
      this.code = '1'
    },
    close() {
      this.noticeForm = {};
      document.getElementById('uploadImg').value = '';
      this.fileNames = []
    },
    onHandle(data) {
      let {row,type} = data;
      console.log(row,type);
      if (type == 'update' || type == 'detail') {
        this.$http(this.$api.notice.info,this.reqData(row.uuid)).then(res => {
          if (res.data.code === 200) {
            this.noticeForm = res.data.data;
            this.fileNames = [this.noticeForm.noticeFile]
            console.log(this.noticeForm);
            if (type == 'update') {
              this.visible = true
            } else {
              this.visible2 = true
            }
          } else {
            this.$message.error(res.data.message)
          }
        })
      } else if (type == 'publish'){
        this.$http(this.$api.notice.publish,this.reqData(row.uuid)).then(res => {
          if (res.data.code === 200) {
            this.$message.success('发布成功')
            this.queryNoticeList();
          }else {
            this.$message.error(res.data.message)
          }
        })
      }
      
    },
    btnClick() {
      console.log(111);
      this.queryNoticeList()
    },
    reset() {
      this.queryForm.createTime = ""
      this.queryNoticeList();
    },
    queryNoticeList() {
      this.queryForm.adminId = this.user.uuid;
      this.queryForm.permission = this.user.permission;
      let url = this.$api.notice.list;
      this.$http(url,this.reqData(this.queryForm,this.pagination.currentPage,this.pagination.pageSize)).then(res => {
        if (res.data.code === 200) {
          let data = res.data.data;
          this.list = data.list;
          this.list.forEach((item,index) => {
            item.rowNum = (this.pagination.currentPage - 1) * this.pagination.pageSize + index + 1
            item.publishFlagName = item.publishFlag == '0' ? '未发布' : '已发布'
          })
          this.total = this.list.length;
          console.log(data);
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    handleSizeChange(val) {
      console.log(val);
      this.pagination.pageSize = val;
      this.queryNoticeList()
    },
    handleCurrentChange(val) {
      console.log(val);
      this.pagination.currentPage = val;
      this.queryNoticeList()
    },
    queryBuildingList() {
      this.$http(this.$api.build.list,this.reqData({},1,999)).then(res => {
        if (res.data.code === 200) {
          let data = res.data.data.list;
          if (this.user.permission == '02') {
            this.buildingList = data.filter(item => item.staffId == this.user.uuid)
          } else {
            this.buildingList = data
          }
          console.log(this.buildingList);
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
  },
  mounted() {
    this.user = JSON.parse(sessionStorage.getItem('user'));
    this.queryBuildingList();
    this.queryNoticeList();
  }
}
</script>
<style lang="less" scoped>
.customerManage {
  background: #fff;
  overflow: hidden;
  padding-bottom: 0.15rem;
}
</style>